package com.ld.collegetask.fragment;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ld.collegetask.R;

abstract public class BaseFragment extends Fragment implements View.OnClickListener {
    protected Activity mActivity;
    protected ImageView img;
    protected TextView title;
    protected TextView text;
    protected ImageView ico;
    protected RelativeLayout rl_loading;
    protected TextView loading_text;
    protected ImageView loading_img;
    private AnimationDrawable animationDrawable;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    protected void setTitleView(Activity mActivity, View view, boolean boImg, boolean boTitle, boolean boTexts, boolean boIco) {
        this.mActivity = mActivity;
        img = (ImageView) view.findViewById(R.id.tv_head_img);
        title = (TextView) view.findViewById(R.id.tv_head_title);
        text = (TextView) view.findViewById(R.id.tv_head_text);
        ico = (ImageView) view.findViewById(R.id.tv_head_ico);
        if (boImg) {
            img.setVisibility(View.VISIBLE);
            img.setOnClickListener(this);
        } else
            img.setVisibility(View.GONE);
        if (boTitle)
            title.setVisibility(View.VISIBLE);
        else
            title.setVisibility(View.GONE);
        if (boTexts) {
            text.setVisibility(View.VISIBLE);
            text.setOnClickListener(this);
        } else
            text.setVisibility(View.GONE);
        if (boIco) {
            ico.setVisibility(View.VISIBLE);
            ico.setOnClickListener(this);
        } else
            ico.setVisibility(View.GONE);
    }

    protected void setLoadingView(Activity mActivity, View view) {
        this.mActivity = mActivity;
        loading_text = (TextView) view.findViewById(R.id.loading_text);
        loading_img = (ImageView) view.findViewById(R.id.loading_img);
        loading_text.setOnClickListener(this);
        rl_loading = (RelativeLayout) view.findViewById(R.id.rl_loading);
        rl_loading.setVisibility(View.GONE);
    }

    protected void loading_go() {
        rl_loading.setVisibility(View.VISIBLE);
        loading_img.setVisibility(View.VISIBLE);
        loading_text.setVisibility(View.INVISIBLE);
        loading_img.setImageResource(R.drawable.loading_http);
        animationDrawable = (AnimationDrawable) loading_img.getDrawable();
        animationDrawable.start();
        new Thread(){
            public void run(){
                try {
                    sleep(350);
                    loading();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }.start();
    }

    protected void loading_end() {
        rl_loading.setVisibility(View.GONE);
        loading_img.setVisibility(View.INVISIBLE);
        loading_text.setVisibility(View.INVISIBLE);
    }

    protected void loading_error() {
        rl_loading.setVisibility(View.VISIBLE);
        loading_img.setVisibility(View.INVISIBLE);
        loading_text.setVisibility(View.VISIBLE);
    }

    abstract public void loading();

    protected void setTitle(String str) {
        title.setText(str);
    }

    protected void setText(String str) {
        text.setText(str);
    }

    protected void setIco(int r) {
        ico.setImageResource(r);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_head_img:
                mActivity.finish();
                break;
            case R.id.loading_text:
                loading_go();
                break;
        }
    }

    //隐藏键盘
    public void hideSoftKeyboard() {
        View a = mActivity.getCurrentFocus();
        if (a != null) {
            InputMethodManager imm = (InputMethodManager) mActivity.getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            try {
                imm.hideSoftInputFromWindow(mActivity.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
